﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Common;

namespace DAL
{
    public class UnCommend
    {
        public Dat_UnCommend SelectUnCommend(int ID)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_UnCommend uncommend = new Dat_UnCommend();
            try
            {
                uncommend = db.Dat_UnCommend.Single(i => i.ID == ID);
            }
            catch (Exception ex)
            {

            }
            return uncommend;
        }

        //select for paging
        public IQueryable<Dat_UnCommend> SelectUnCommend(int StartRowIndex, int MaxRows, string TOPIC, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
            int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

            IQueryable<Dat_UnCommend> Query;

            if (UserType == 1)
            {
                Query = from i in GetConditionQuery(TOPIC, OneDate, TwoDate)
                            where i.USERKEY == UserKey
                            orderby i.CREATETIME descending
                            select i;
            }
            else
            {
                Query = from i in GetConditionQuery(TOPIC, OneDate, TwoDate)
                            orderby i.CREATETIME descending
                            select i;
            }
            return Query.Skip(StartRowIndex).Take(MaxRows);         
        }

        public int SelectUnCommendCount(int StartRowIndex, int MaxRows, string TOPIC, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            return (from i in GetConditionQuery(TOPIC, OneDate, TwoDate)
                        select i).Count();
        }

        private IQueryable<Dat_UnCommend> GetConditionQuery(string TOPIC, string OneDate, string TwoDate)
        {
            CIMDataContext db = new CIMDataContext();

            var predicate = PredicateBuilder.True<Dat_UnCommend>();

            if (!string.IsNullOrEmpty(TOPIC))
            {
                predicate = predicate.And(i => i.TOPIC.Contains(TOPIC.Trim()));
            }
            if (!string.IsNullOrEmpty(OneDate))
            {
                try
                {
                    DateTime onedate = Convert.ToDateTime(OneDate);
                    predicate = predicate.And(i => i.CREATETIME >= onedate);
                }
                catch (Exception ex) { }
            }
            if (!string.IsNullOrEmpty(TwoDate))
            {
                try
                {
                    DateTime twodate = Convert.ToDateTime(TwoDate);
                    predicate = predicate.And(i => i.CREATETIME <= twodate);
                }
                catch (Exception ex) { }
            }

            return db.Dat_UnCommend.Where(predicate);
        }

        public bool AddUnCommend(Dat_UnCommend uncommend)
        {
            CIMDataContext db = new CIMDataContext();
            db.Dat_UnCommend.InsertOnSubmit(uncommend);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditUnCommend(Dat_UnCommend uncommend)
        {
            CIMDataContext db = new CIMDataContext();

            try
            {
                Dat_UnCommend Uncommand = db.Dat_UnCommend.Single(u => u.ID == uncommend.ID);

                Uncommand.TOCONTENT = uncommend.TOCONTENT;
                Uncommand.STATE = uncommend.STATE;

                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public void DelUnCommend(int ID)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_UnCommend uncommend = db.Dat_UnCommend.Single(i => i.ID == ID);
            db.Dat_UnCommend.DeleteOnSubmit(uncommend);
            db.SubmitChanges();
        }
    }
}
